package org.jbit.news.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jbit.news.biz.NewsBiz;
import org.jbit.news.biz.TopicBiz;
import org.jbit.news.biz.impl.NewsBizImpl;
import org.jbit.news.biz.impl.TopicBizImpl;
import org.jbit.news.entity.News;
import org.jbit.news.entity.Topic;
import org.jbit.news.util.Page;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;



public class NewsIndexServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//		request.setCharacterEncoding("utf-8");
//		response.setCharacterEncoding("utf-8");
		String ntid = request.getParameter("tid");
		
		HttpSession session =request.getSession();
		session.setAttribute("ntid", ntid);
		System.out.println("dddddddd:      "+ntid);
		NewsBiz nb = new NewsBizImpl();
		//--------------------------
		String xg=request.getParameter("xg");
   		
   		/*if(ntid==null||ntid.trim().length()==0){ntid="-1";}*/
   		String ntitle=(String)request.getAttribute("ntitle");
   		String nauthor=(String)request.getAttribute("nauthor");
   		String nsummary=(String)request.getAttribute("nsummary");
   		String ncontent=(String)request.getAttribute("ncontent");
    	String nid=request.getParameter("nid");
    	String  tid=request.getParameter("tid");
    	session.setAttribute("tid", tid);
    	session.setAttribute("ntitle", ntitle);
    	session.setAttribute("nauthor", nauthor);
    	session.setAttribute("nsummary", nsummary);
    	session.setAttribute("ncontent", ncontent);
    	session.setAttribute("nid", nid);
    	PrintWriter out=response.getWriter();
		
		Page p = new Page();
		//国内的侧边栏
		  List<News> newsChina=nb.getNewsByTopicTid(1);
		  session.setAttribute("newChinaList", newsChina);
		  //国际的侧边栏
		  List<News> newsinv=nb.getNewsByTopicTid(2);
		  session.setAttribute("newsinvList", newsinv);
		  //娱乐的侧边栏
		  List<News> newsYule=nb.getNewsByTopicTid(5);
		  session.setAttribute("newYuleList", newsYule);
		  //获取所有的主题
		  TopicBiz tb=new TopicBizImpl();
    		List<Topic> listTo=tb.getAllTopics();
    		System.out.println("cccccccccc:           "+listTo.get(0).getTid());
    		session.setAttribute("listTo", listTo);
		
		String currNostr = request.getParameter("currNo");
		if (currNostr == null || currNostr.trim().length() == 0) {
			currNostr = "1";
		}
		int currNo = Integer.parseInt(currNostr);
		if (currNo < 1) {
			currNo = 1;
		}
		if (ntid == null || ntid.trim().length() ==0) {
			
			int totalCount = nb.getTotalCount();// 总计录数
			p.setPageSize(10);// 每页显示条数
			p.setTotalCount(totalCount);// 获取总记录数
			if (currNo > p.getTotalPageCount()) {
				currNo = p.getTotalPageCount();
			}
			p.setCurrPageNo(currNo);// 查看的第几页
			List<News> list = nb.getPageNewsList(Integer.parseInt(ntid),p.getCurrPageNo(),
					p.getPageSize());
			p.setNewsList(list);
//			session.setAttribute("pagelist",list);
//			session.setAttribute("page", p);
			//josn格式输入
			String  newsJSON=JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteNullListAsEmpty);
			out.print(newsJSON);
			
			//end  josn
//			response.sendRedirect("index.jsp");
		}else {
			
//			List<News> newsTopic=nb.getNewsByTopicTid(Integer.parseInt(ntid));
//			System.out.println("zzzzzzzzzzzz");
			int totalCountTopic= nb.getTotalCountByNtid(Integer.parseInt(ntid));// 总计录数
			p.setPageSize(10);// 每页显示条数
		
			if (currNo > p.getTotalPageCount()) {
				currNo = p.getTotalPageCount();
			}
			p.setCurrPageNo(currNo);// 查看的第几页
			List<News> list = nb.getPageNewsList(Integer.parseInt(ntid),p.getCurrPageNo(),
					p.getPageSize());
			p.setNewsList(list);
//			session.setAttribute("newsTopic", newsTopic);
//			request.getRequestDispatcher("index.jsp").forward(request, response);
			String  newsJSON=JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteNullListAsEmpty);
			out.print(newsJSON);
		}
		out.flush();
		out.close();
		
	}
}
